package com.rayboo.example.collections;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * PriorityQueue 示例
 *
 * @author ray
 * @since 2019/07/25
 *
 * <p>https://beginnersbook.com/2017/08/java-collections-priorityqueue-interface/
 */
public class PriorityQueueExample {

    public static void main(String[] args) {

        PriorityQueue<String> queue = new PriorityQueue<String>(15, new MyComparator());
        queue.add("Tyrion Lannister");
        queue.add("Daenerys Targaryen");
        queue.add("Arya Stark");
        queue.add("Petyr 'Littlefinger' Baelish");

        /*
         * What I am doing here is removing the highest
         * priority element from Queue and displaying it.
         * The priority I have set is based on the string
         * length. The logic for it is written in Comparator
         */
        while (queue.size() != 0) {
            System.out.println(queue.poll());

        }
    }

    public static class MyComparator implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            return o1.length() - o2.length();
        }
    }
}
